var source = require('./source/testHistorySource.js');
var async = require('async');
var request = require('request');
var schedule = require('node-schedule');
//var readability = require('node-readability-cheerio');
var cheerio = require('cheerio');
//var idxSymbolMap = require('./QuotesUtilities.js').idxSymbolMap;
var mysqlDao = require('./dao/mysqlDao.js');
function unique(arr, data) {
    var l = arr.length;
    for (var i = 0; i < l; i++) {
        if (arr[i].symbol == data.symbol && arr[i].date == data.date) {
            return true;
        }
    }
    arr.push(data);
}
function unique2(arr) {
    var r = [];
    var l = arr.length;
    for (var i = 0; i < l; i++) {
        unique(r, arr[i]);
    }
    return r;
}
function date2strlocal(dateObj, format) {
    var yyyy = dateObj.getFullYear();
    var mm = dateObj.getMonth() + 1;
    var dd = dateObj.getDate();
    if (mm < 10) {
        mm = '0' + mm;
    }
    if (dd < 10) {
        dd = '0' + dd;
    }

    switch (format) {
        case 'yyyymmdd':
            return yyyy + '-' + mm + '-' + dd;
            break;
        case 'mmddyyyy':
            return mm + '-' + dd + '-' + yyyy;
            break;
        default :
            return yyyy + '-' + mm + '-' + dd;
    }
}
function extractForm($, headSelector, rowSelector, eleSelector) {
    var headArray = [];
    if (headSelector instanceof Array) {
        headArray = headSelector;
    }
    else if (typeof headSelector === 'string') {
        var head = $(headSelector).children(eleSelector);
        for (var j = 0; j < head.length; j++) {
            headArray[j] = $(head[j]).text();
        }
    }
    else {
        throw new Error('Error type of headSelector');
    }

    var rows = $(rowSelector);
    var colNum = headArray.length;
    var rowNum = rows.length;
    var result = [];
    for (var j = 0; j < rowNum; j++) {
        var obj = {};
        for (var k = 0; k < colNum; k++) {
            var elements = $(rows[j]).children(eleSelector);
            if (headArray[k] == 'date') {
                obj[headArray[k]] = Number($(elements[k]).attr('data-real-value')) * 1000;
            } else {
                obj[headArray[k]] = $(elements[k]).text().trim();
            }
        }
        result.push(obj);
    }
    return result;
}
function date2strutc(dateObj, format) {
    var yyyy = dateObj.getUTCFullYear();
    var mm = dateObj.getUTCMonth() + 1;
    var dd = dateObj.getUTCDate();
    if (mm < 10) {
        mm = '0' + mm;
    }
    if (dd < 10) {
        dd = '0' + dd;
    }

    switch (format) {
        case 'yyyymmdd':
            return yyyy + '-' + mm + '-' + dd;
            break;
        case 'mmddyyyy':
            return mm + '-' + dd + '-' + yyyy;
            break;
        default :
            return yyyy + '-' + mm + '-' + dd;
    }
}
function getLength(str) {
    str = String(str);
    if (str.length <= 1) {
        str = '0' + str;
    }
    return str
}
function getHisQuotes_Investing(options) {
    console.log(options);
    var now = new Date();
    // 只爬每个月的数据
    var staetNow = now.getTime() - 24 * 60 * 60 * 1000 * 30;
    var startTime = new Date(staetNow);
    var start = getLength(startTime.getMonth() + 1) + '/' + getLength(startTime.getDate()) + '/' + startTime.getFullYear();
    var end = getLength(now.getMonth() + 1) + '/' + getLength(now.getDate()) + '/' + now.getFullYear();
    start = '01/01/2000';
    async.mapLimit(options, 4, function (item, cb) {
        var form = {
            action: 'historical_data',
            curr_id: item.investingID,
            st_date: start,
            end_date: end,
            interval_sec: 'Monthly'
        };
        var reqOptions = {
            url: 'https://www.investing.com/instruments/HistoricalDataAjax',
            headers: {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
                'X-Requested-With': 'XMLHttpRequest'
            },
            timeout: 20000,
            form: form
        };
        request.post(reqOptions, function (err, res, body) {
            if (err) {
                console.log('#####' + reqOptions.form.curr_id);
                cb(err);
                return;
            }
            console.log('#####' + reqOptions.form.curr_id);
            var $ = cheerio.load(body);
            if ($('#curr_table > tbody tr').text().indexOf('No results found') != -1) {
                return cb(null, []);
            }
            var result = extractForm($,
                ['date', 'close', 'open', 'high', 'low', 'volume', 'changeinpercent'],
                '#curr_table > tbody tr', 'td');
            for (var j = 0; j < result.length; j++) {
                result[j].symbol = item.etf_symbol;
                result[j].name = item.name;
                result[j].date = date2strlocal(new Date(result[j].date));
                result[j].nameen = '';
                result[j].namecn = '';
                result[j].country = '';
                if (result[j].volume == '-') {
                    result[j].volume = '';
                }
            }
            setTimeout(function () {
                cb(null, result);
            }, 500);
        });
    }, function (err, data) {
        if (err) {
            console.log(err);
            return;
        }
        var arr = [];
        var l = data.length;
        for (var i = 0; i < l; i++) {
            arr = arr.concat(data[i]);
        }
        //console.log(data);
        arr = unique2(arr);
        //console.log(arr.length);
        mysqlDao.toHistoryMonthly(arr);
    });
}

getHisQuotes_Investing(source.slice(158, source.length));
// console.log('historyByMonthly.js 线程');
// var rule = new schedule.RecurrenceRule();
// var times = [];
// var l = 24;
// for (var i = 0; i < l; i += 2) {
//     times.push(i);
// }
// rule.hour = times;
// rule.minute = 40;
// var j = schedule.scheduleJob(rule, function () {
//     getHisQuotes_Investing(source);
// });